<?php
	include_once("db/UserDAO.inc");
	include_once("db/AccessLevel.inc");
	include_once("UserView.php");
	
	$u = null;

	if (isset($_REQUEST["uid"])) {
		$u = User::getUser($_REQUEST["uid"]);
	}
	
	$isNewUser = $u == null;
	$title = ($isNewUser) ? "Add New User" : "Edit User - $u->username";
	
	if ($isNewUser) {
		$u = new User();
	}
?>

<script language="javascript"><!--
	function validateUserForm() {
		var username = document.getElementById("username").value;
		var p1 = document.getElementById("p1").value;
		var p2 = document.getElementById("p2").value;
		var firstName = document.getElementById("firstName").value;
		var surname = document.getElementById("surname").value;
		var ecno = document.getElementById("ecno").value;
		
		if (username.length == 0) {
			alert("Please provide a User Name.");
			return;
		}
		
		if (p1 != p2) {
			alert("Supplied passwords do not match.  Try again.");
			return;
		}
		
		if (p1.length == 0) {
			alert("Please provide a Password.");
			return;
		}
		
		if (firstName.length == 0) {
			alert("Please provide a First Name.");
			return;
		}
		
		if (surname.length == 0) {
			alert("Please provide a Surname.");
			return;
		}
		
		document.getElementById("userForm").submit();
	}
--></script>

<h1><?php print $title?></h1>
	
<form id="userForm" method="post" action="?action=setUser">
	<input type="hidden" name="uid"
			value="<?php print ($isNewUser) ? 0 : $u->uid ?>" />

	<table width="40%" cellspacing="0" class="formLayout">
		<?php if (isset($_REQUEST["error"]) && $_REQUEST["error"] == "userExists") { ?>
			<tr>
				<td colspan="2" class="error">
					The user name you have entered already exists.  Please
					use another one.
				</td>
			</tr>
		<?php } ?>
	
		<tr>
			<td>User Name</td>
			<td><input type="text" id="username" name="username"
					<?php print (!$isNewUser) ? "readonly" : "" ?> 
					value="<?php print $u->username ?>" /></td>
		</tr>
		
		<tr>
			<td>Password</td>
			<td><input id="p1" type="password" 
					name="password" value="<?php print $u->password ?>"/></td>
		</tr>
		
		<tr>
			<td>Confirm Password</td>
			<td><input id="p2" type="password" 
					value="<?php print $u->password ?>"/></td>
		</tr>
		
		<tr>
			<td>First Name</td>
			<td><input type="text" id="firstName" name="firstName" 
					value="<?php print $u->firstName ?>" /></td>
		</tr>
		
		<tr>
			<td>Surname</td>
			<td><input type="text" id="surname" name="surname"
					value="<?php print $u->surname ?>" /></td>
		</tr>
		
		<tr>
			<td>EC #</td>
			<td><input type="text" id="ecno" name="ecno"
					value="<?php print $u->ecno ?>" /></td>
		</tr>

		<tr>
			<td>Access Level</td>
			<td>
				<?php showAccessLevelList($_SESSION["accessLevel"], $u->accessLevel) ?>
			</td>
		</tr>
		
		<tr>
			<td>Shift</td>
			<td><input type="text" id="shift" name="shift" maxlength="1"
					value="<?php print $u->shift ?>" /></td>
		</tr>
		
		<tr colspan="2">
			<td>
				<input type="button" value="Confirm" class="button"
						onClick="javascript:validateUserForm()" />
				<input type="button" class="button" value="Cancel" onClick="window.location.href='.'"/></a>
			</td>
		</tr>
	</table> 
</form>
